package com.refusea.train.mapper

import com.refusea.train.domain.entity.School
import org.apache.ibatis.annotations.Delete
import org.apache.ibatis.annotations.Insert
import org.apache.ibatis.annotations.Mapper
import org.apache.ibatis.annotations.Options
import org.apache.ibatis.annotations.Param
import org.apache.ibatis.annotations.Select
import org.apache.ibatis.annotations.Update

@Mapper
interface SchoolMapper {

    @Insert("INSERT ignore INTO school (name, principal_id) VALUES (#{name}, #{principalId})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    fun insert(school: School): Int

    @Delete("DELETE FROM school WHERE id = #{id}")
    fun delete(id: Int): Int

    @Update("UPDATE school SET name = #{name}, principal_id = #{principalId} WHERE id = #{id}")
    fun update(school: School): Int

    @Select("SELECT * FROM school WHERE id = #{id}")
    fun select(id: Int): School?

    @Select("SELECT * FROM school")
    fun list(): List<School>

    @Update("UPDATE school SET principal_id = #{principalId} WHERE id = #{id}")
    fun setPrincipal(@Param("id") id: Int, @Param("principalId") principalId: Int): Int

    @Select("SELECT * FROM school")
    fun listAll(): List<School>
}
